package com.software.car.controller;

import com.software.car.api.ModelService;
import com.software.car.common.utils.CommonPage;
import com.software.car.common.utils.CommonResult;
import com.software.car.common.utils.SnowflakeIdGenerator;
import com.software.car.entity.dto.InsertModelDto;
import com.software.car.entity.vo.ModelOptionVo;
import com.software.car.entity.vo.ModelVo;
import com.software.car.entity.vo.ModelSimpleVo;
import com.software.car.entity.po.ModelPo;
import com.software.car.entity.po.UserPo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.Date;
import java.util.List;

/**
 * 模板管理
 *
 * @Author 魔法はまだ解けない
 * @Date 2021/2/3
 */
@RestController
@RequestMapping("/model")
@Validated
public class ModelController {

    @Autowired
    ModelService modelService;

    /**
     *
     * 公共
     * 获取模板列表/查询        OK
     *
     * @param state
     * @param type
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/public")
    @PreAuthorize("hasAnyRole('ROLE_USER','ROLE_ADMINISTER','ROLE_SUPER_ADMINISTER')")
    public CommonResult getSimpleModel(@RequestParam(name = "state", required = false) Integer state,
                                       @RequestParam(name = "type", required = false) Integer type,
                                       @RequestParam(name = "page", required = false) Integer page,
                                       @RequestParam(name = "pageSize", required = false) Integer pageSize,
                                       @RequestParam(name = "name", required = false) String name) {
        if (page == null || pageSize == null) {
            List<ModelSimpleVo> simpleModel = modelService.getSimpleModel(state, type,name);
            return CommonResult.success(simpleModel);
        }
        CommonPage commonPage = modelService.getSimpleModel(state, type, page, pageSize,name);
        return CommonResult.success(commonPage);
    }

    /**
     * 管理员
     * 创建模板         OK
     *
     * @param insertModelDto
     * @param userPo
     * @return
     */
    @PostMapping("/administer")
    @PreAuthorize("hasAnyRole('ROLE_ADMINISTER','ROLE_SUPER_ADMINISTER')")
    public CommonResult addModel(@RequestBody InsertModelDto insertModelDto,
                                 @RequestAttribute("user") UserPo userPo) {
        //新模板数据初始化

        insertModelDto.setCreateUser(userPo.getId());
        insertModelDto=modelService.addInit(insertModelDto);
        String s = modelService.addModel(insertModelDto);
        return CommonResult.success("模板id："+s);
    }

    /**
     *
     * 公共
     * 根据modelId获取模板详细信息      OK
     *
     * @param modelId
     * @param state 选项状态
     * @return
     */
    @GetMapping("/public/optionDetail")
    public CommonResult getModelForm(@RequestParam(name = "modelId") @NotBlank String modelId, Integer state) {
        ModelVo model = modelService.getModel(modelId,state);
        //获取状态的模板
        return CommonResult.success(model);
    }

    /**
     * 修改模板基本信息
     *
     * @return
     */
    @PutMapping("/administer")
    @PreAuthorize("hasAnyRole('ROLE_ADMINISTER','ROLE_SUPER_ADMINISTER')")
    public CommonResult updateModel(@RequestBody InsertModelDto insertModelDto,
                                    @RequestAttribute("user") UserPo userPo) {
        //设置新的修改人、修改时间
        insertModelDto.setUpdateUser(userPo.getId());
        insertModelDto.setUpdateTime(new Date());

        insertModelDto = modelService.updateInit(insertModelDto);
        String s = modelService.updateModel(insertModelDto);
        return CommonResult.success("模板id："+s);
    }

    /**
     * 管理员
     * 修改模块状态           OK
     *
     * @param modelId 模块id
     * @param state   状态 1创建完成，2使用中，3禁用
     * @return
     */
    @PutMapping("/administer/state")
    @PreAuthorize("hasAnyRole('ROLE_ADMINISTER','ROLE_SUPER_ADMINISTER')")
    public CommonResult banModel(@RequestParam @NotBlank String modelId,
                                 @RequestParam @Max(message = "不符合范围",value = 3) @Min(message = "不符合范围",value = 2) Integer state,
                                 @RequestAttribute("user") UserPo userPo) {
        ModelPo modelPo = new ModelPo();
        modelPo.setId(modelId);
        modelPo.setState(state);
        return CommonResult.success(modelService.updateModel(modelId, state, userPo.getId()));
    }
    /**
     * 管理员
     * 删除状态为1的模块状态           OK
     *
     * @param modelId 模块id
     * @return
     */
    @DeleteMapping("/administer")
    @PreAuthorize("hasAnyRole('ROLE_ADMINISTER','ROLE_SUPER_ADMINISTER')")
    public CommonResult deleteModel(@RequestParam @NotBlank String modelId) {
        modelService.deleteModel(modelId);
        return CommonResult.success();
    }

}
